<template>
	<view>
		<view class="">
			<input class="uni-input" type="text" v-model="dataList.title" />
			<input class="uni-input" type="text" v-model="dataList.subTitle" />
			<input class="uni-input" type="text" v-model="dataList.neirong" />
			<button type="default" @tap="addList">新增</button>
		</view>
		<view class="" v-for="(item,index) of list" :key='index'>
			<view class="">标题: {{item.title}}</view>
			<view class="">小标题: {{item.subTitle}}</view>
			<view class="">内容: {{item.neirong}}</view>
			<view class="">日期: {{item.data}}</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				dataList: {
					title: '',
					subTitle: '',
					neirong: '',
					data: ''
				},
				list: []
			}
		},
		onLoad() {
			this.getTextList()
		},
		methods: {
			addList() {
				uni.showLoading({
				  title: '处理中...'
				})
				uniCloud.callFunction({
				  name: 'addText',
				  data: this.dataList
				}).then((res) => {
				  uni.hideLoading()
				  // uni.showModal({
				  //   content: `成功添加一条数据，文档id为：${res.result.id}`,
				  //   showCancel: false
				  // })
				  console.log(res)
				  this.getTextList()
				}).catch((err) => {
				  uni.hideLoading()
				  uni.showModal({
				    content: `添加数据失败，错误信息为：${err.message}`,
				    showCancel: false
				  })
				  console.error(err)
				})
			},
			
			getTextList() {
				uni.showLoading({
				  title: '处理中...'
				})
				uniCloud.callFunction({
				  name: 'getText'
				}).then((res) => {
				  uni.hideLoading()
				  // uni.showModal({
				  //   content: `查询成功，获取数据列表为：${JSON.stringify(res.result.data)}`,
				  //   showCancel: false
				  // })
				  console.log(res)
				  this.list = res.result.data
				}).catch((err) => {
				  uni.hideLoading()
				  uni.showModal({
				    content: `查询失败，错误信息为：${err.message}`,
				    showCancel: false
				  })
				  console.error(err)
				})
			},
		}
	}
</script>

<style>
	input{
		border: 1rpx solid red;
		padding: 10rpx;
		margin-top: 20rpx;
	}
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>
